#pragma once

#include <string>
#include "USBDevice.h"
#include "USBHub.h"

#include <WINDOWS.H>
#include <usbioctl.h>

#define USBSuperSpeed (UsbHighSpeed + 1)
#define USBSuperSpeedPlus (UsbHighSpeed + 2)

//class USBHub;

class USBPort
{
public:
	USBPort(void);
	~USBPort(void);

	int								GetPortNumber()			{ return m_portNumber; }
	USB_CONNECTION_STATUS			GetStatus()				{ return m_portStatus; }
	std::wstring					GetHubDevicePath()		{ return m_portHubDevicePath; }
	USB_DEVICE_SPEED				GetSpeed()				{ return m_portSpeed; }
	bool							IsHub()					{ return m_portIsHub; }
	bool							IsDeviceConnected()		{ return m_portIsDeviceConnected; }
	const USB_DEVICE_DESCRIPTOR*	GetDeviceDescriptor()	{ return &m_portDeviceDescriptor; }

	void	SetPortNumber(const int portNumber)								{ m_portNumber = portNumber; }
	void	SetStatus(const USB_CONNECTION_STATUS status)					{ m_portStatus = status; SetDeviceConnected(m_portStatus == DeviceConnected); }
	void	SetHubDevicePath(const wchar_t *pHubDevicePath)					{ m_portHubDevicePath = pHubDevicePath; }
	void	SetSpeed(const USB_DEVICE_SPEED speed)							{ m_portSpeed = speed; }
	void	SetDeviceIsHub(const bool isHub)								{ m_portIsHub = isHub; }
	void	SetDeviceConnected(const bool deviceConnected)					{ m_portIsDeviceConnected = deviceConnected; }
	void	SetDeviceDescriptor(const USB_DEVICE_DESCRIPTOR &descriptor)	{ memcpy(&m_portDeviceDescriptor, &descriptor, sizeof(USB_DEVICE_DESCRIPTOR)); }

	USBDevice*	GetDevice();
	USBHub*		GetHub();

private:
	int						m_portNumber;
	USB_CONNECTION_STATUS	m_portStatus;
	std::wstring			m_portHubDevicePath;
	USB_DEVICE_SPEED		m_portSpeed;
	bool					m_portIsHub;
	bool					m_portIsDeviceConnected;
	USB_DEVICE_DESCRIPTOR	m_portDeviceDescriptor;

	USBDevice				*m_pUSBDevice;
	USBHub					*m_pUSBHub;

	void			DeleteDevice();
	void			DeleteHub();
	std::wstring	GetDescriptorString(HANDLE hHub, UCHAR descriptorCode);
	std::wstring	GetDescriptionByKeyName(wchar_t *driverKeyName);
	std::wstring	GetInstanceIDByKeyName(wchar_t *driverKeyName);
};
